package com.misboot.document.controller;

import cn.ewsd.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import com.misboot.document.utils.BaseUtils;
import com.misboot.document.utils.PageUtil.PageParam;
import com.misboot.document.utils.PageUtil.PageSet;
import com.misboot.document.model.OssFile;
import com.misboot.document.service.OssFileService;

/**
 * oss业务附件表
 *
 * @Author 湖南佐佑时代科技有限公司
 * @Email zuoyou@.com
 * @Date 2020-10-26 17:13:21
 */
@Api(tags = {"oss业务附件表"})
@Controller
@RequestMapping("/ossFile")
public class OssFileController extends DocumentBaseController {

    @Autowired
    private OssFileService ossFileService;

    @Autowired
    private AliyunOssController aliyunOssController;

    @ApiOperation(value = "获得OssFile分页集数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", required = true, dataType = "Int"),
            @ApiImplicitParam(name = "rows", value = "一页显示多少条记录", defaultValue = "20", required = true, dataType = "Int"),
            @ApiImplicitParam(name = "sort", value = "排序", defaultValue = "createTime", required = true, dataType = "String"),
            @ApiImplicitParam(name = "order", value = "排序规则", defaultValue = "desc", required = true, dataType = "String"),
    })
    @ResponseBody
    @RequestMapping(value = "/getPageSet", method = RequestMethod.POST)
    public Object getPageSet(PageParam pageParam, String puuid) {
        String filterSort = "";
        String puuidStr = "";
        if (!StringUtils.isNullOrEmpty(puuid)) {
            puuidStr = " puuid ='" + puuid + "' and ";
        }
        filterSort = puuidStr + BaseUtils.filterSort(request, filterSort);
        PageSet<OssFile> pageSet = ossFileService.getPageSet(pageParam, filterSort);
        return pageSet;
    }

    @ApiOperation(value = "获得OssFile模块详细数据")
    @ApiImplicitParam(name = "uuid", value = "获得OssFile模块详细数据", required = true, dataType = "String")
    @ResponseBody
    @RequestMapping(value = "/getDetailByUuid")
    public Object getDetailByUuid(String uuid) {
        OssFile ossFile = ossFileService.selectByPrimaryKey(uuid);
        return ossFile;
    }


    @ApiOperation(value = "保存OssFile模块数据")
    @ApiImplicitParam(name = "ossFile", value = "保存OssFile模块数据", required = true, dataType = "OssFile")
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(@ModelAttribute OssFile ossFile) {
        int result = ossFileService.insertSelective(getSaveData(ossFile));
        return result > 0 ? success("保存成功！") : failure("保存失败！");
    }


    @ApiOperation(value = "更新OssFile模块数据")
    @ApiImplicitParam(name = "ossFile", value = "更新OssFile模块数据", required = true, dataType = "OssFile")
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object update(@ModelAttribute OssFile ossFile) {
        int result = ossFileService.updateByPrimaryKeySelective(getUpdateData(ossFile));
        return result > 0 ? success("更新成功！") : failure("更新失败！");
    }

    @ApiOperation(value = "删除OssFile模块数据")
    @ApiImplicitParam(name = "ossFile", value = "删除OssFile模块数据", required = true, dataType = "OssFile")
    @ResponseBody
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.POST)
    public Object deleteBatch(@RequestParam String[] uuid) {
        int result = ossFileService.executeDeleteBatch(uuid);
        return result > 0 ? success("删除成功！") : failure("删除失败！");
    }

    @ResponseBody
    @RequestMapping(value = "/deleteFile", method = RequestMethod.POST)
    public Object deleteFile(@RequestParam String[] uuid, String endpoint, String bucketName,String keyUrl) {
        int result = ossFileService.executeDeleteBatch(uuid);
        if(result>0){
            String[] urls = new String[1];
            urls[0] = keyUrl;
            aliyunOssController.download(urls, endpoint, bucketName);
        }
        return result > 0 ? success("删除成功！") : failure("删除失败！");
    }


}
